using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace OnlineAnadolu
{
    public partial class menemen : System.Web.UI.Page
    {
        int a = 0;
     
        string[] b = new string[100];
        Database db = new Database();
        void FillAuthors(MenuItem node)
        {
            string connString = System.Configuration.ConfigurationSettings.AppSettings["MyCon"];
            SqlConnection connection = new SqlConnection(connString);
            SqlCommand command = new SqlCommand("Select * From categories where parentid is null", connection);
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            DataSet authors = new DataSet();
            adapter.Fill(authors);
            if (authors.Tables.Count > 0)
            {
                foreach (DataRow row in authors.Tables[0].Rows)
                {
                    MenuItem newNode = new MenuItem();
                    newNode.Text = row["categoryname"].ToString();
                    newNode.Value = (row["categoryid"]).ToString();

                    // newNode.NavigateUrl = "../UrunGoster.aspx?CatID=" + (row["categoryid"]).ToString();

                    // newNode.SelectAction = TreeNodeSelectAction.Expand;
                    node.ChildItems.Add(newNode);

                    FillTitlesForAuthors(newNode);

                }
            }
        }

        void FillTitlesForAuthors(MenuItem node)
        {
            string authorID = node.Value.ToString();
            string connString = System.Configuration.ConfigurationSettings.AppSettings["MyCon"];
            SqlConnection connection = new SqlConnection(connString);
            SqlCommand command = new SqlCommand("Select categoryname, categoryid From categories Where parentid = '" + authorID + "'", connection);
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            DataSet titlesForAuthors = new DataSet();
            adapter.Fill(titlesForAuthors);
            if (titlesForAuthors.Tables.Count > 0)
            {
                foreach (DataRow row in titlesForAuthors.Tables[0].Rows)
                {
                    MenuItem newNode = new MenuItem();
                    newNode.Text = row["categoryname"].ToString();
                    newNode.Value = (row["categoryid"]).ToString();
                    //    newNode.NavigateUrl = "../UrunGoster.aspx?CatID=" + (row["categoryid"]).ToString();

                    // newNode.PopulateOnDemand = false;
                    //   newNode.SelectAction = TreeNodeSelectAction.None;
                    node.ChildItems.Add(newNode);
                    //node.NavigateUrl = "../UrunGoster.aspx?CatID=" + (row["categoryid"]).ToString();

                    FillTitlesForAuthors(newNode);
                }
            }
        }
        void click()
        {
            Response.Write(Menu1.SelectedItem.Text);
        }
        ////int olacak()
        ////{
        ////    Label1.Text = "";
        ////    string query = "Select * from categories where parentid=" + Menu1.SelectedValue + "";

        ////    DataTable dt = db.select(query);
        ////    if (dt.Rows.Count != 0)
        ////        Label1.Text = "Kategorilendirmede sonuna kadar gitmediniz";
        ////    else
        ////    {
        ////        // Label1.Text = " "+ Menu1.SelectedItem.Text+Label1.Text;
        ////        yolbul(Convert.ToInt32(Menu1.SelectedValue));
        ////    }
        ////    return 0;
        ////}
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Label1.Text != "")
            {

                //Panel1.Controls.Clear();
                Label1.Text = "";
                string query = "Select * from categories where parentid=" + Menu1.SelectedValue + "";

                DataTable dt = db.select(query);
                if (dt.Rows.Count != 0)
                    Label1.Text = "Kategorilendirmede sonuna kadar gitmediniz";
                else
                {
                    // Label1.Text = " "+ Menu1.SelectedItem.Text+Label1.Text;
                    yolbul(Convert.ToInt32(Menu1.SelectedValue));
                }
            }
            
            if (!IsPostBack)
            {
                FillAuthors(Menu1.Items[0]);
            }
        }
        int yolbul(int childid)
        {
            string query1 = "Select * from categories where categoryid=" + childid + "";
            DataTable dt = db.select(query1);

            foreach (DataRow dr in dt.Rows)
            {
               Label1.Text = ">>>" + dr["categoryname"].ToString() + Label1.Text;
                string query2 = "SELECT attributevalues.attributeid,attributetypes.datatype, attributes.attributename FROM attributes INNER JOIN attributevalues ON attributes.attributeid = attributevalues.attributeid INNER JOIN attributetypes ON attributes.type = attributetypes.attributecode WHERE(attributes.categoryid = " + dr["categoryid"].ToString() + ") group by attributevalues.attributeid,attributetypes.datatype,attributes.attributename";
                DataTable dt2 = new DataTable();
                dt2 = db.select(query2);
                if (dt2.Rows.Count != 0)
                {
                    
                    foreach (DataRow dr2 in dt2.Rows)
                    {
                        Label lr = new Label();
                        lr.Text = dr2["attributename"].ToString();
                        Panel1.Controls.Add(lr);
                        string query3 = "SELECT   attributevalues.value FROM attributes INNER JOIN attributevalues ON attributes.attributeid = attributevalues.attributeid INNER JOIN attributetypes ON attributes.type = attributetypes.attributecode WHERE(attributes.categoryid = " + dr["categoryid"].ToString() + ") and attributevalues.attributeid='" + dr2["attributeid"].ToString() + "'";
                        DataTable dt3 = new DataTable();
                        dt3 = db.select(query3);
                        
                            if(dr2["datatype"].ToString()=="combobox")
                                {
                                  b[a] = "combobox";
                                    DropDownList dwn2 = new DropDownList();
                             
                                    dwn2.ID = a.ToString();
                                   
                                    Panel1.Controls.Add(dwn2);
                                    ((DropDownList)Panel1.FindControl(a.ToString())).DataTextField = "value";
                                
                                    ((DropDownList)Panel1.FindControl(a.ToString())).DataSource = dt3;
                                    ((DropDownList)Panel1.FindControl(a.ToString())).BackColor = System.Drawing.Color.Blue;
                                    ((DropDownList)Panel1.FindControl(a.ToString())).DataBind();
                                    
                                     a++;
                             

                                }
                                if (dr2["datatype"].ToString() == "checkbox")
                                {
                                    b[a] = "checkbox";
                                    CheckBoxList ch = new CheckBoxList();


                                    ch.ID = a.ToString();
                                    
                                    Panel1.Controls.Add(ch);
                                    // ch.Attributes = dt3;
                                    ((CheckBoxList)Panel1.FindControl(a.ToString())).DataTextField = "value";

                                    ((CheckBoxList)Panel1.FindControl(a.ToString())).DataSource = dt3;
                                    ((CheckBoxList)Panel1.FindControl(a.ToString())).BackColor = System.Drawing.Color.Blue;
                                    ((CheckBoxList)Panel1.FindControl(a.ToString())).DataBind();
                                   a++;
                                    Panel1.DataBind();
                                    //Label2.Text = Label2.Text + " " + ((CheckBoxList)Panel1.FindControl((a-1).ToString())).SelectedItem.Text;


                                }

                        
                      

                    }
                }
                if (dr["parentid"].ToString() == "")
                    return 0;

                yolbul(Convert.ToInt32(dr["parentid"]));
            }

            return 0;

        }
        protected void click(object sender, MenuEventArgs e)
        {
            //Panel1.Controls.Remove(((DropDownList)Panel1.FindControl("1")));

          // Panel1.Controls.Clear();
            Menu1.Enabled = false;

            Label1.Text = "";
                string query = "Select * from categories where parentid=" + Menu1.SelectedValue + "";

                DataTable dt = db.select(query);
                if (dt.Rows.Count != 0)
                    Label1.Text = "Kategorilendirmede sonuna kadar gitmediniz";
                else
                {
                    // Label1.Text = " "+ Menu1.SelectedItem.Text+Label1.Text;
                    yolbul(Convert.ToInt32(Menu1.SelectedValue));
                }
               

            
            //Response.Write(Menu1.SelectedItem.Text);



        }

        protected void bt_Kaydet_Click(object sender, EventArgs e)
        {
            ////Label1.Text = "";
            ////string query = "Select * from categories where parentid=" + Menu1.SelectedValue + "";

            ////DataTable dt = db.select(query);
            ////if (dt.Rows.Count != 0)
            ////    Label1.Text = "Kategorilendirmede sonuna kadar gitmediniz";
            ////else
            ////{
            ////    // Label1.Text = " "+ Menu1.SelectedItem.Text+Label1.Text;
            ////    yolbul(Convert.ToInt32(Menu1.SelectedValue));
            ////}

               // Label2.Text = Label2.Text + " " + ((DropDownList)Panel1.FindControl("6")).SelectedItem.Text;
            for (int i = 0; i < a; i++)
            {
              if(b[i] == "combobox")              
                    Label2.Text = Label2.Text + " " + ((DropDownList)Panel1.FindControl(i.ToString())).SelectedItem.Text;
                if (b[i] == "checkbox")
                    Label2.Text = Label2.Text + " " + ((CheckBoxList)Panel1.FindControl(i.ToString())).SelectedItem.Text;

            }
            Label1.Text = "";
        }

        protected void page_load(object sender, MenuEventArgs e)
        {

        }

        protected void Page_Load(object sender, MenuEventArgs e)
        {

        }
        protected void bt_Tamam_Click(object sender, EventArgs e)
        {
            Response.Redirect("menemen.aspx");
        }
        
      
    }
}
